package com.example.programming.sys.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.programming.sys.entity.StudentGrade;
import com.example.programming.sys.mapper.CourseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.*;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.StudentMapper;
import com.example.programming.sys.entity.Student;

import com.example.programming.common.tools.BaseController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 学生表 前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/sys/student")
public class StudentController extends BaseController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseMapper courseMapper;
    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Student> page = new Page<Student>(pageNum, pageSize);
        Student parms = new Student();
        QueryWrapper<Student> warpper = new QueryWrapper<Student>(parms);

        result.setData(studentMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Student entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            studentMapper.insert(entity);
        } else {
            studentMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        studentMapper.deleteBatchIds(deleteIds);
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/get")
    public Result get(String stuid) {
        Result result = new Result();
        Student parms = new Student();
        parms.setStuid(stuid);
        parms.setLearned(1);
        QueryWrapper<Student> warpper = new QueryWrapper<Student>(parms);
        result.setData(studentMapper.selectList(warpper));
        return result;
    }
    /**
     * 功能描述: 导出Excel(下载)
     *
     * @auther: zhuxuan
     * @date: 2019/12/23
     * @param: [response, fileName, beanType, data, arr]
     * @return: void
     */
    @GetMapping("export/{id}")
    public  void exportExcel(HttpServletResponse response,@PathVariable("id")String id)  {
        try(ServletOutputStream out = response.getOutputStream();
            ) {

            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            List<Student> list = studentMapper.selectList(new QueryWrapper<Student>().eq("couid",id));
            List<StudentGrade> data = new LinkedList<>();
            for(Student student:list) {
                StudentGrade studentGrade = new StudentGrade();
                BeanUtils.copyProperties(student,studentGrade);
                data.add(studentGrade);
            }
            Sheet sheet1 = new Sheet(1,0, StudentGrade.class);
            sheet1.setSheetName("sheet");
            writer.write(data, sheet1);
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode( courseMapper.selectById(id).getCourse()+".xlsx", "utf-8"));
            writer.finish();
            out.flush();
        } catch (Exception e) {

        } finally {

        }
    }
}
